package com.houzehui.mncommodity.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.houzehui.mncommodity.config.JwtUtils;
import com.houzehui.mncommodity.config.MsgResult;
import com.houzehui.mncommodity.entity.User;
import com.houzehui.mncommodity.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author houzehui
 * @since 2022-04-08
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService iUserService;

    @Autowired
    JwtUtils jwtUtils;

    @GetMapping("login")
    public MsgResult login(User user){

        System.err.println(user);
        User uername = iUserService.getBaseMapper().selectOne(new QueryWrapper<User>().eq("username", user.getUsername()));

        if(uername==null){
            return MsgResult.fail("用户名不存在");
        }

        if(!uername.getPwd().equals(user.getPwd())){
            return MsgResult.fail("密码错误");
        }

        String token = jwtUtils.createToken(JSON.toJSONString(uername));

        return MsgResult.succeed(token);


    }

}
